package Test;

import java.io.*;
import java.net.*;
import java.util.*;

public class Client {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        Socket mysocket=null;
        DataInputStream in=null;
        DataOutputStream out=null;
        Thread readData ;
        Read read=null;
        try{  mysocket=new Socket();
            read = new Read();
            readData = new Thread(read);
            System.out.print("输入服务器的IP:");
            String IP = scanner.nextLine();
            System.out.print("输入端口号:");
            int port = scanner.nextInt();
            if(mysocket.isConnected()){}
            else{
                InetAddress  address=InetAddress.getByName(IP);
                InetSocketAddress socketAddress=new InetSocketAddress(address,port);
                mysocket.connect(socketAddress);
                in =new DataInputStream(mysocket.getInputStream());
                out = new DataOutputStream(mysocket.getOutputStream());
                read.setDataInputStream(in);
                readData.start();
            }
        }
        catch(Exception e) {
            System.out.println("服务器已断开"+e);
        }
        System.out.print("输入算式：");
        while(scanner.hasNext()) {
            String s = new String();
            try {
                s = scanner.nextLine();
                System.out.println(s);

            }
            catch(InputMismatchException exp){
                System.exit(0);
            }
            try {
                out.writeUTF(s);
            }
            catch(Exception e) {}
        }
    }
}
